package eai.tp2.lpco.database;



import java.io.Serializable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;


@Entity
@Table(name = "ORDERS")
@OnDelete(action = OnDeleteAction.CASCADE)
public class Order implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public enum Status { DELIVERED, PENDING }

	private int orderId;
	private User user;
	private Date date;
	private Status status;
	private List<DigitalCamera> digitalCameras;
	private int amount;

	
	public Order(){
		
		
	}
	
	public Order(User user, Date date, Status state) {
		this.user = user;
		this.date = date;
		this.status = state;
		this.digitalCameras = new ArrayList<DigitalCamera>();

	}



	@Column(name = "ORDERID")
	@Id
	@GeneratedValue
	public int getOrderId() {
		return orderId;
	}



	public void setOrderId(int id) {
		this.orderId = id;
	}
	
	public User getUser() {
		return user;
	}

	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="username")
	public void setUser(User user) {
		this.user = user;
	}

	@Column(name = "DATE")
	public Date getDate() {
		return date;
	}



	public void setDate(Date date) {
		this.date = date;
	}


	@Column(name = "STATUS")
	public Status getStatus() {
		return status;
	}



	public void setStatus(Status status) {
		this.status = status;
	}
	
	@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name="cameraid")
	public List<DigitalCamera> getDigitalCameras() {
		return digitalCameras;
	}

	public void setDigitalCameras(List<DigitalCamera> digitalCameras) {
		this.digitalCameras = digitalCameras;
	}
	
	public int getAmount() {
		return amount;
	}


	public void setAmount(int amount) {
		this.amount = amount;
	}



	public String toString() {
		String res = "Order: " + orderId + "\n";
		//res += "Purchaser: " + this.user.getFullName() + "\n";
		//res += "Number of Cameras: " + this.cameras.size() + "\n";
		res += "Date: " + this.date.toString() + "\n";
		res += "State: " + this.status + "\n";

		return res;
	}

}
